package com.ruoyi.business.service.wecom.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.domain.dto.admin.wecom.WecomGroupPageQueryDTO;
import com.ruoyi.business.domain.vo.wecom.admin.ChatGroupDetailVO;
import com.ruoyi.business.domain.vo.wecom.admin.ChatGroupMemberVO;
import com.ruoyi.business.domain.wecom.TbWecomChatGroup;
import com.ruoyi.business.domain.wecom.TbWecomChatGroupMember;
import com.ruoyi.business.mapper.wecom.TbWecomChatGroupMapper;
import com.ruoyi.business.mapper.wecom.TbWecomChatGroupMemberMapper;
import com.ruoyi.business.service.wecom.ITbWecomChatGroupService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TbWecomChatGroupServiceImpl extends ServiceImpl<TbWecomChatGroupMapper, TbWecomChatGroup> implements ITbWecomChatGroupService {

    @Resource
    private TbWecomChatGroupMemberMapper tbWecomChatGroupMemberMapper;

    @Override
    public TableDataInfo<TbWecomChatGroup> sysPageQuery(WecomGroupPageQueryDTO pageQueryDTO, PageQuery pageQuery) {
        LambdaQueryWrapper<TbWecomChatGroup> qwl = Wrappers.lambdaQuery(TbWecomChatGroup.class)
            .like(StringUtils.isNotEmpty(pageQueryDTO.getChatId()), TbWecomChatGroup::getChatId, pageQueryDTO.getChatId())
            .like(StringUtils.isNotEmpty(pageQueryDTO.getChatName()), TbWecomChatGroup::getChatName, pageQueryDTO.getChatName())
            .like(StringUtils.isNotEmpty(pageQueryDTO.getChatOwner()), TbWecomChatGroup::getChatOwner, pageQueryDTO.getChatOwner())
            .orderByDesc(TbWecomChatGroup::getChatCreateTime);
        Page<TbWecomChatGroup> page = this.page(pageQuery.build(), qwl);
        return TableDataInfo.build(page);
    }

    @Override
    public ChatGroupDetailVO sysSelectDetail(String chatId) {
        TbWecomChatGroup chatGroup = this.getOne(Wrappers.lambdaQuery(TbWecomChatGroup.class)
            .orderByDesc(TbWecomChatGroup::getChatCreateTime)
            .eq(TbWecomChatGroup::getChatId, chatId)
            .last(Constants.MYBATIS_PLUSH_LIMIT_ONE)
        );
        if (ObjectUtil.isEmpty(chatGroup)) {
            throw new ServiceException("客户群不存在");
        }
        ChatGroupDetailVO res = BeanUtil.toBean(chatGroup, ChatGroupDetailVO.class);
        List<TbWecomChatGroupMember> tbWecomChatGroupMembers = tbWecomChatGroupMemberMapper.selectList(Wrappers.lambdaQuery(TbWecomChatGroupMember.class)
            .eq(TbWecomChatGroupMember::getChatId, chatId)
            .orderByDesc(TbWecomChatGroupMember::getJoinTime)
        );
        List<ChatGroupMemberVO> chatGroupMemberVOS = BeanUtil.copyToList(tbWecomChatGroupMembers, ChatGroupMemberVO.class);
        res.setChatGroupMembers(chatGroupMemberVOS);
        return res;
    }
}
